//
//  ActionSheetView.m
//  objective_c_language
//
//  Created by 王玉龙 on 2018/6/1.
//  Copyright © 2018年 longlong. All rights reserved.
//

#import "ActionSheetView.h"
#import <Masonry.h>

@interface ActionSheetView()

{
    CGFloat _buttonHeight;
}

@property (nonatomic ,strong) NSArray<NSString *> *titleArray;
@property (nonatomic, strong) NSString *title;

/** 内容 */
@property (nonatomic, strong) UIView *contentView;


/** 遮盖 */
@property (nonatomic, strong) UIButton *coverButton;

@property (nonatomic ,strong) SelectButton buttonBlock;


@end

@implementation ActionSheetView

/** 动画时间 */
static CGFloat const AnimationDuration = 0.2;

- (instancetype)initWithTitles:(NSArray<NSString *> *)titleArray ButtonClick:(SelectButton)block
{
    self = [super init];
    
    if (self)
    {
        self.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight); // 设置self的frame， 若没有设置button的点击事件不响应（想要响应button的点击事件， 其父视图必须有frame且大于button）
        _buttonBlock = block;
        _titleArray = titleArray;
        _buttonHeight = 50;
        self.textColor = COLOR_VALUE(0x4A4A4A, 1);
        self.backgroundColor = [UIColor clearColor];
        [[[UIApplication sharedApplication] keyWindow] addSubview:self];
        
        [self addSubview:self.coverButton];
        _coverButton.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        
    }
    
    return self;
}
- (instancetype)initWithTitle:(NSString *)title actionTitles:(NSArray<NSString *> *)titleArray ButtonClick:(SelectButton)block {
    self = [super init];
    
    if (self)
    {
        self.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight); // 设置self的frame， 若没有设置button的点击事件不响应（想要响应button的点击事件， 其父视图必须有frame且大于button）
        _buttonBlock = block;
        _titleArray = titleArray;
        _title = title;
        _buttonHeight = 50;
        self.textColor = COLOR_VALUE(0x4A4A4A, 1);
        self.backgroundColor = [UIColor clearColor];
        [[[UIApplication sharedApplication] keyWindow] addSubview:self];
        
        [self addSubview:self.coverButton];
        _coverButton.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        
    }
    
    return self;
}

- (UIButton *)coverButton
{
    if (_coverButton == nil)
    {
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        button.backgroundColor = COLOR_VALUE(0x333333, 0.6);
        button.alpha = 0.0;
        [button addTarget:self action:@selector(dismissPickerView) forControlEvents:UIControlEventTouchUpInside];
        
        _coverButton = button;
    }
    
    return _coverButton;
}


- (UIView *)contentView
{
    if (_contentView == nil)
    {
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = COLOR_VALUE(0xFAFAFA, 1);
        float floatsortaPixel =1.0/[UIScreen mainScreen].scale;
        
        float headHeight = 0;
        if (self.title) {
            UILabel *titleLabel = [[UILabel alloc] init];
            titleLabel.backgroundColor = [UIColor whiteColor];
            titleLabel.text = self.title;
            titleLabel.textColor = COLOR_VALUE(0x7B7E85, 1);
            titleLabel.font = [UIFont systemFontOfSize:15];
            titleLabel.textAlignment = NSTextAlignmentCenter;
            [view addSubview:titleLabel];
            headHeight = 40;
            [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(0);
                make.left.mas_equalTo(0);
                make.right.mas_equalTo(0);
                make.height.mas_equalTo(40);
            }];
            
            UIView *headSeperateLine = [[UIView alloc] init];
            headSeperateLine.backgroundColor = CUT_LINE_COLOR;
            [view addSubview:headSeperateLine];
            [headSeperateLine mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(39);
                make.left.mas_equalTo(0);
                make.right.mas_equalTo(0);
                make.height.mas_equalTo(floatsortaPixel);
            }];
        }
        
        UIButton *cancelButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        cancelButton.backgroundColor = [UIColor whiteColor];
        cancelButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [cancelButton setTitleColor:COLOR_VALUE(0xcccccc, 1) forState:UIControlStateNormal];
        [cancelButton addTarget:self action:@selector(dismissPickerView) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:cancelButton];
        [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.bottom.mas_equalTo(0);
            make.right.mas_equalTo(0);
            make.height.mas_equalTo(_buttonHeight);
        }];
        
        UIView *lineView1 = [UIView new];
        lineView1.backgroundColor = CUT_LINE_COLOR;
        [view addSubview:lineView1];
        [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(floatsortaPixel);
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
            make.bottom.equalTo(cancelButton.mas_top);
        }];
        
        // RGBA(204, 204, 204, 1)
        [_titleArray enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)
         {
             CGFloat top = idx * (_buttonHeight + 1) + headHeight;
             
             UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
             button.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:16];
             button.tag = idx + 1;
             button.backgroundColor = [UIColor whiteColor];
             [button setTitle:obj forState:UIControlStateNormal];
             [button setTitleColor:self.textColor forState:UIControlStateNormal];
             [button addTarget:self action:@selector(buttonTapClick:) forControlEvents:UIControlEventTouchUpInside];
             [view addSubview:button];
             [button mas_makeConstraints:^(MASConstraintMaker *make) {
                 make.top.mas_equalTo(top);
                 make.left.mas_equalTo(0);
                 make.right.mas_equalTo(0);
                 make.height.equalTo(cancelButton.mas_height);
             }];
             
             UIView *lineView2 = [UIView new];
             lineView2.backgroundColor = CUT_LINE_COLOR;
             [view addSubview:lineView2];
             [lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
                 make.top.equalTo(button.mas_bottom);
                 make.height.mas_equalTo(floatsortaPixel);
                 make.left.mas_equalTo(0);
                 make.right.mas_equalTo(0);
             }];
         }];
        
        view.frame = CGRectMake(0, ScreenWidth, ScreenHeight, 10 + (_buttonHeight + 1) * (_titleArray.count + 1) + headHeight);
        _contentView = view;
    }
    
    return _contentView;
}

- (void)buttonTapClick:(UIButton *)sender
{
    _buttonBlock(sender.tag);
    [self dismissPickerView];
}

#pragma mark - Public Method

// 出现
- (void)show
{
    [self addSubview:self.contentView];
    
    [UIView animateWithDuration:AnimationDuration animations:^
     {
         self.coverButton.alpha = 1.0;
         self.coverButton.transform = CGAffineTransformMakeTranslation(0,  -CGRectGetHeight(self.contentView.frame));
         self.contentView.transform = CGAffineTransformMakeTranslation(0,  -CGRectGetHeight(self.contentView.frame));
     }];
}

// 消失
- (void)dismissPickerView
{
    [UIView animateWithDuration:AnimationDuration animations:^
     {
         self.contentView.transform = CGAffineTransformMakeTranslation(0,  CGRectGetHeight(self.contentView.frame));
         self.coverButton.transform = CGAffineTransformMakeTranslation(0,  CGRectGetHeight(self.contentView.frame));
         self.coverButton.alpha = 0.0;
     } completion:^(BOOL finished) {
         [self.contentView removeFromSuperview];
         [self.coverButton removeFromSuperview];
         [self removeFromSuperview];
     }];
}

@end

